<template>
  <div class="container-comment">
    <el-card>
      <div slot="header">
        <my-bread>评论管理</my-bread>
      </div>
      <!-- 表格 -->
      <el-table :data="comments">
        <el-table-column label="文章标题" prop="title" width="300px"></el-table-column>
        <el-table-column label="评论数" prop="total_comment_count"></el-table-column>
        <el-table-column label="粉丝评论数" prop="fans_comment_count"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">{{scope.row.comment_status?'已打开':'已关闭'}}</template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.comment_status"
              type="danger"
              size="small"
              @click="toggleStatus(scope.row)"
            >关闭评论</el-button>
            <el-button @click="toggleStatus(scope.row)" v-else type="success" size="small">打开评论</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px"
        background
        layout="prev, pager, next"
        @current-change="changePager"
        :page-size="reqParams.per_page"
        :current-page="reqParams.page"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  name: "comment-page",
  data() {
    return {
      comments: [],
      total: 0,
      //   请求参数对象
      reqParams: {
        page: 1,
        per_page: 20,
        response_type: "comment"
      }
    };
  },
  created() {
    this.getComments();
  },
  methods: {
    //   获取评论列表
    async getComments() {
      const {
        data: { data }
      } = await this.$http.get("/articles", { params: this.reqParams });
      this.comments = data.results;
      this.total = data.total_count;
    },
    // 分页功能实现
    changePager(newPage) {
      this.reqParams.page = newPage;
      this.getComments();
    },
    // 文章评论状态
    toggleStatus(row) {
      // 切换弹出询问框
      const openText = "确定开启文章评论功能???";
      const closeText = "关闭文章评论功能???";
      this.$comfirm(row.comment_status ? closeText : openText, "友情提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        try {
          const {
            data: { data }
          } = await this.$http.put(`/comments/status?article_id=${row.id}`, {
            allow_comment: !row.comment_status
          });
          // data.allow_comment  修改后文章的评论状态
          this.$message.success(
            data.allow_comment ? "打开评论成功" : "关闭评论成功"
          );
          // 更新列表当前文章状态
          row.comment_status = data.allow_comment;
        } catch (e) {
          this.$message.error("操作失败");
        }
      }).catch(()=>{})
    }
  }
};
</script>
<style scoped lang='less'>
</style>